
package com.ce.cases.source.action;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.ce.cases.report.logic.IReportIllegalLogic;
import com.ce.cases.source.dao.po.CaseSource;
import com.ce.cases.source.dao.po.CaseSourceApprove;
import com.ce.cases.source.dao.po.CaseSourceDrawApprove;
import com.ce.cases.source.dao.po.CaseSourceFiles;
import com.ce.cases.source.dao.po.DjNsrxx;
import com.ce.framework.constants.WebConstants;
import com.ce.framework.datamaintain.action.DataMaintainAction;
import com.ce.framework.datamaintain.dao.po.AbstractPO;
import com.ce.framework.exception.AppException;
import com.ce.framework.util.BeanAccessUtil;
import com.ce.framework.util.DateUtil;
import com.ce.framework.util.GeneratorUUID;
import com.ce.framework.util.LogUtil;
import com.ce.framework.util.StringUtil;
import com.ce.framework.util.node.JsonTreeNode;
import com.ce.sys.login.model.User;
import com.ce.sys.org.dao.po.SysVOrg;
import com.ce.sys.person.dao.po.SysPerson;
import com.ce.util.DbUtil;
import com.ce.util.FlowPicView;
import com.ce.util.FlowWorkUtil;
import com.ce.util.OrgUtil;
import com.ce.workflow.work.dao.po.WorkflowFlow;
import com.ce.workflow.work.dao.po.WorkflowWork;


/**
* 嫦娥软件有限公司 版权所有 2013
* 创建人：　andy
* 创建日期：2014-03-18
* 创建时间：15:29:27
* 功能描述：备选案源台账 [CASE_SOURCE]更改时用的Action
* ==============================================
* 修改历史
* 修改人		修改时间		修改原因
*
* ==============================================
*/
public class CaseSourceDrawApproveAction extends DataMaintainAction 
{
	private static final long serialVersionUID = 1L;
	
	private CaseSourceApprove appr;
	private CaseSourceDrawApprove drawAppr;
	private IReportIllegalLogic reportIllegalLogic;
	@Override
	protected void initOtherData(boolean isNew) {
		if(isNew){
			try {
				BeanAccessUtil.copyProperty(po, "registerPerson", User.getUserName());
				BeanAccessUtil.copyProperty(po, "registerDate", new Date());
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			
		}
	}
	
	public String openTqPage(){
		initOtherData(true);
		try {
			return this.add();
		} catch (AppException e) {
			e.printStackTrace();
		}
		return "";
	}
	@Override
	protected String addItem() throws AppException {
		String inspectionDeptId=this.getParameter("po.inspectionDeptId");
		String collectionDeptId=this.getParameter("po.collectionDeptId");
		String jcjName=OrgUtil.getOrgName(inspectionDeptId);
		String zgjName=OrgUtil.getOrgName(collectionDeptId);
		try {			
			BeanAccessUtil.copyProperty(po, "registerDeptId", User.getUserOrgId());
			BeanAccessUtil.copyProperty(po, "registerDept", User.getUserOrgName());			
			BeanAccessUtil.copyProperty(po, "state", "0");
			BeanAccessUtil.copyProperty(po, "inspectionDept", jcjName);
			BeanAccessUtil.copyProperty(po, "collectionDept", zgjName);
			BeanAccessUtil.copyProperty(po, "vcAaccept", "0");	//初始为未受理。		
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
		return super.addItem();
	}
	@Override
	protected String modifyItem() throws AppException {
		//避免将流程状态暴漏给页面，避免非法篡改
		CaseSourceDrawApprove entiy=(CaseSourceDrawApprove)this.baselogic.getPOByPk(po.getClass().getName(), po.getVcId());
		BeanAccessUtil.copyNoCopyNullProperty(po, entiy);
		CaseSourceDrawApprove po2=(CaseSourceDrawApprove)po;
		String jcjName=OrgUtil.getOrgName(po2.getInspectionDeptId());
		String zgjName=OrgUtil.getOrgName(po2.getCollectionDeptId());
		entiy.setInspectionDept(jcjName);
		entiy.setCollectionDept(zgjName);
		this.baselogic.updatePo(entiy,this.getAddList(),this.getModifyList(),this.getDeleteList());
	    return super.searchData();			
	}
	@Override
	public String look() throws AppException {
		super.look();
//		GtSafeTConstructionMonth po=(GtSafeTConstructionMonth)this.getPo();
//		
//		List<SysDictItem> l=DictUtil.getDictListByName("施工管理-施工等级");
//		for(SysDictItem item:l){
//			if(item.getVcItemNumber().equals(po.getVcSgdj())){
//				po.setVcSgdj(item.getVcItemName());
//				break;
//			}
//		}
		return WebConstants.REQUEST_LOOKDATA_OPERATOR;
	}
	public String openShPage(){
		return "openShPage";
	}
	
	/**
	 * 重新受理
	 * @return
	 */
	public String saveNewAcceptSuccessedBefore(){
		String[] vcIds=this.getParameterValues("chkSelf[]");
		String acceptType=this.getParameter("acceptType");//是否受理
		String sql_ids=StringUtil.fromArrayToStr(vcIds, true);
		List<CaseSourceDrawApprove> l=new ArrayList<CaseSourceDrawApprove>();
		try {
			l = (List<CaseSourceDrawApprove>)this.baselogic.searchByHql("FROM CaseSourceDrawApprove p where p.vcDelFlag='0' and p.vcId in("+sql_ids+")");
		} catch (AppException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		CaseSourceDrawApprove po=new CaseSourceDrawApprove();
		if(l!=null&&l.size()>0){
			if("1".equals(acceptType))//受理
			{
				for(CaseSourceDrawApprove p:l){
					try {
						List<CaseSource> pos=(List<CaseSource>)this.baselogic.searchByHql("FROM CaseSource p where p.vcDelFlag='0' and p.taxpayerNumber='"+p.getTaxpayerNumber()+"'");
						if(pos!=null&&pos.size()>0){
							CaseSource temp=pos.get(0);
							if(3==temp.getState())
								return this.returnInfoForJS(false,"纳税人识别号为"+p.getTaxpayerNumber()+",涉案企业"+ p.getInvolvedEnterprice()+"已在已选库，确定受理吗？");
							else
								return this.returnInfoForJS(false,"纳税人识别号为"+p.getTaxpayerNumber()+",涉案企业"+ p.getInvolvedEnterprice()+"已在备选库，确定受理吗？");
						}
					} catch (AppException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		}
		
		return this.returnInfoForJS(true,"");
	}
	
	/**
	 * 重新受理
	 * @return
	 */
	public String saveNewAcceptSuccessed(){
		//String vcId=this.getParameter("chkSelf");
		String[] vcIds=this.getParameterValues("chkSelf[]");
		String acceptType=this.getParameter("acceptType");//是否受理
		CaseSourceDrawApprove po=new CaseSourceDrawApprove();
		
		String sql_ids=StringUtil.fromArrayToStr(vcIds, true);
		List<CaseSourceDrawApprove> l=new ArrayList<CaseSourceDrawApprove>();
		try {
			l = (List<CaseSourceDrawApprove>)this.baselogic.searchByHql("FROM CaseSourceDrawApprove p where p.vcDelFlag='0' and p.vcId in("+sql_ids+")");
		} catch (AppException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if(l!=null&&l.size()>0){
			for(CaseSourceDrawApprove p:l){
				p.setVcAaccept(acceptType);
				p.setOperatePersonName(User.getUserName());
				p.setOperatePerson(User.getUserId());
				p.setOperateDate(new Date());
				
				if("1".equals(acceptType)){//受理involvedEnterprice
					
					boolean b=true;
					if(StringUtil.isNotBlank(p.getCaseId())){
						CaseSource case_po=new CaseSource();
						try {
							case_po=(CaseSource)this.baselogic.getPOByPk(case_po.getClass().getName(), p.getCaseId());
						} catch (AppException e) {
							e.printStackTrace();
						}
						if(case_po!=null){
							b=false;
							case_po.setVcAaccept("1");//受理		
							case_po.setVcDelFlag(0);
							try {
								this.baselogic.updatePo(case_po);
							} catch (AppException e) {
								e.printStackTrace();
							}
						}								
						
					}					
					if(b){
						CaseSource case_po=new CaseSource();
						case_po.setVcId(GeneratorUUID.generateUUID());
						case_po.setVcDelFlag(0);
						case_po.setVcType("2");//跨区局提请
						case_po.setState(0);
						case_po.setVcAaccept("1");//已受理
						case_po.setRegisterDept(p.getRegisterDept());
						case_po.setRegisterDeptId(p.getRegisterDeptId());
						case_po.setRegisterPerson(p.getRegisterPerson());
						case_po.setReceiveDate(new Date());
						case_po.setRegisterDate(new Date());
						case_po.setInspectionDeptId(p.getInspectionDeptId());
						case_po.setInspectionDept(p.getInspectionDept());
						case_po.setCollectionDeptId(p.getCollectionDeptId());
						case_po.setCollectionDept(p.getCollectionDept());
						case_po.setTaxpayerNumber(p.getTaxpayerNumber());
						case_po.setInvolvedEnterprice(p.getInvolvedEnterprice());
						case_po.setCaseFromNum(p.getCaseFromNum());
						case_po.setCaseFromName(p.getCaseFromName());
						case_po.setIllegalTypeNum(p.getIllegalTypeNum());
						case_po.setIllegalTypeName(p.getIllegalTypeName());
						case_po.setIsImportant(p.getIsImportant());
						case_po.setIsSecret(p.getIsSecret());				
						case_po.setBaseResult(p.getDrawReason());						
						p.setCaseId(case_po.getVcId());						
						try {
							this.baselogic.addPo(case_po);
						} catch (AppException e) {
							e.printStackTrace();
						}		
					}
					
					
				}else if("2".equals(acceptType)){
					if(StringUtil.isNotBlank(p.getCaseId())){
						CaseSource case_po=new CaseSource();
						try {
							case_po=(CaseSource)this.baselogic.getPOByPk(case_po.getClass().getName(), p.getCaseId());
						} catch (AppException e) {
							e.printStackTrace();
						}
						case_po.setVcAaccept("2");//不予受理					
						try {
							this.baselogic.updatePo(case_po);
						} catch (AppException e) {
							e.printStackTrace();
						}		
						
					}
				}
				try {
					this.baselogic.updatePo(p);
				} catch (AppException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}		
			}
		}		
		return this.returnInfoForJS(true, "操作成功！");
	}
	
	public String openSubmitPage(){
		
		String vcId=this.getParameter("chkSelf");
		
		CaseSourceDrawApprove po=null;
		try {
			po = (CaseSourceDrawApprove) this.baselogic.getPOByPk(CaseSourceDrawApprove.class.getName(), vcId);
		} catch (AppException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(1==po.getState()){
			return this.alertMsgAndExecJs("不能重复操作，该案源已上报！", "$('#operateWindow').window('close');");
		}
		List<SysPerson> persons=OrgUtil.getUserByOrgId("15009070700", true);
		this.setAttribute("persons", persons);
		this.setAttribute("vcId", vcId);
		return "caseSourceSubmit";
	}
	
	public String submitSuccessed(){
		String vcRecordId=this.getParameter("chkSelf");
		String vcHandlePersonId=this.getParameter("vcHandlePersonId");
		String vcHandlePersonName=this.getParameter("vcHandlePersonName");
		CaseSourceDrawApprove po=new CaseSourceDrawApprove();
		User user=User.getUser();
		try {
			po=(CaseSourceDrawApprove)this.baselogic.getPOByPk(po.getClass().getName(), vcRecordId);
		} catch (AppException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		po.setState(1);//上报到分管局长
		
		String vcHandleOrgId="",vcHandleOrgName="";
		String vcDesc=this.getParameter("vcAdvance");		
		vcHandleOrgId=OrgUtil.getOrgIdByUserId(vcHandlePersonId);
		vcHandleOrgName=OrgUtil.getOrgNameByUserId(vcHandlePersonId);
		FlowWorkUtil.submitFlowWork("CASE_SOURCE",vcRecordId,"1",user.getVcOrgId(),user.getOrgName(),user.getVcId(),user.getVcName(), vcHandleOrgId, vcHandleOrgName, vcHandlePersonId, vcHandlePersonName, vcDesc);			
		try {
			this.baselogic.updatePo(po);
		} catch (AppException e) {
			e.printStackTrace();
		}
		
		return this.returnInfoForJS(true, null);		
	}
	
	public String lookItemFrame(){
		return "lookItemFrame";
	}
	
	
	public String ajaxPersons(){
		String vcOrgId=this.getParameter("vcOrgId");
		List<SysPerson> persons=OrgUtil.getUserByOrgId(vcOrgId, true);
		this.rows=persons;
		return JSON;
	}
	public String ajaxOrgs(){
		String vcOrgId=this.getParameter("vcOrgId");
		List<SysVOrg> orgs=OrgUtil.getChildDep(vcOrgId);
		this.rows=orgs;
		return JSON;
	}
		
	
	/**
	 * 打开案源下发界面
	 * @return
	 */
	public String caseSourceDown(){
		String vcId=this.getParameter("chkSelf");
		String vcParId=User.getParOrgIdByOrgLevel("1");
		List<SysVOrg> deps=OrgUtil.getChildPro(vcParId);
		StringBuffer op = new StringBuffer();
		if(deps!=null&&deps.size()>0){
			for(SysVOrg org:deps){
				    op.append("<option value='"+org.getVcId()+"'>"+org.getVcName()+"</option>");
			}
			
		}else{
			op.append("<option value=''></option>");
		}
		this.setAttribute("deps", deps);
		this.setAttribute("vcId", vcId);
		return "caseSourceDown";
	}
	
	/*
	 * 处理审批页面
	 */
	public String dealApprovePage() throws AppException{
		String caseId=this.getParameter("caseId");
		CaseSource source=(CaseSource)this.baselogic.getPOByPk("com.ce.cases.source.dao.po.CaseSource", caseId);
		List approveList=this.baselogic.searchByHql("From CaseSourceApprove po where po.caseId='"+caseId+"' and po.vcDelFlag=0");
		if(!approveList.isEmpty()){
			appr=(CaseSourceApprove)approveList.get(0);
		}else{
			appr=new CaseSourceApprove();
			appr.setCaseId(source.getVcId());
			appr.setCaseFrom(source.getCaseFromName());
			appr.setCaseNum(source.getCaseNumber());
			appr.setInvolvedEnterprice(source.getInvolvedEnterprice());
			appr.setTaxpayerNumber(source.getTaxpayerNumber());
		}
		return "dealApprovePage";
	}
	
	/*
	 * 处理审批操作
	 */
	public String dealApprove() {
		try {
			// 修改审批表
			if (StringUtil.isNotBlank(appr.getVcId())) {
				appr.setVcDelFlag(0);
				this.baselogic.updatePo(appr);
			} else {// 添加审批表
				appr.setVcId(GeneratorUUID.generateUUID());
				appr.setVcDelFlag(0);
				this.baselogic.addPo(appr);
			}
		} catch (AppException e) {
			LogUtil.logError(e);
			return this.returnInfoForJS(false,
					e.getMessage().replaceAll("'|\\n", " "));
		}
		return this.returnInfoForJS(true, null);
	}
	
	/*
	 * 提请审批页面
	 */
	public String drawApprovePage() throws AppException{
		String caseId=this.getParameter("caseId");
		CaseSource source=(CaseSource)this.baselogic.getPOByPk("com.ce.cases.source.dao.po.CaseSource", caseId);
		List drawApprList=this.baselogic.searchByHql("From CaseSourceDrawApprove po where po.caseId='"+caseId+"' and po.vcDelFlag=0");
		if(!drawApprList.isEmpty()){
			drawAppr=(CaseSourceDrawApprove)drawApprList.get(0);
		}else{
			drawAppr=new CaseSourceDrawApprove();
			drawAppr.setCaseId(caseId);
			drawAppr.setCaseFromNum(source.getCaseFromNum());
			drawAppr.setCaseFromName(source.getCaseFromName());
			drawAppr.setTaxpayer(source.getInvolvedEnterprice());
			drawAppr.setTaxpayerNumber(source.getTaxpayerNumber());
			drawAppr.setDrawDept(User.getUserOrgName());
			drawAppr.setDrawDeptId(User.getUserOrgId());
			drawAppr.setInspectionDept(source.getInspectionDept());
			drawAppr.setInspectionDeptId(source.getInspectionDeptId());
			drawAppr.setCollectionDept(source.getCollectionDept());
			drawAppr.setCollectionDeptId(source.getCollectionDeptId());
			drawAppr.setIllegalTypeNum(source.getIllegalTypeNum());
			drawAppr.setIllegalTypeName(source.getIllegalTypeName());
		}
		return "drawApprovePage";
	}
	
	/*
	 * 提请审批操作
	 */
	public String drawApprove() throws AppException{
		try {
			// 修改审批表
			if (StringUtil.isNotBlank(drawAppr.getVcId())) {
				drawAppr.setVcDelFlag(0);
				this.baselogic.updatePo(drawAppr);
			} else {// 添加审批表
				drawAppr.setVcId(GeneratorUUID.generateUUID());
				drawAppr.setVcDelFlag(0);
				drawAppr.setDrawDeptId(User.getUserOrgId());
				this.baselogic.addPo(drawAppr);
			}
		} catch (AppException e) {
			LogUtil.logError(e);
			return this.returnInfoForJS(false,
					e.getMessage().replaceAll("'|\\n", " "));
		}
		return this.returnInfoForJS(true, "提请审批操作成功!");
	}
	public String getNsrinfoByTaxpayerNumber(){
    	String taxpayerNumber=this.getParameter("taxpayerNumber");
    	DjNsrxx po=reportIllegalLogic.getDjNsrxxRemote(taxpayerNumber);
    	
    	if(null!=po){
    		SysVOrg jcjorg=OrgUtil.getParOrg(po.getZgswjDm());
    		if(jcjorg!=null){
    			po.setZgswjMc(OrgUtil.getOrgName(po.getZgswjDm()));
            	po.setJcjdm(jcjorg.getVcId());
            	po.setJcjmc(jcjorg.getVcName());
            	String year=DateUtil.getYear();
            	String sql="select count(*) from CASE_SOURCE t where t.INSPECTION_DEPT_ID='"+jcjorg.getVcId()+"' and t.vc_del$flag='0'";
            	int cnt=DbUtil.getFirstIntSqlQuery(sql);
            	String cnt_="";
            	if(cnt<10)
            		cnt_="00"+cnt;
            	else if(cnt<100)
            		cnt_="0"+cnt;
            	else 
            		cnt_=""+cnt;
            	po.setStatusInfo(year+"DX-J"+jcjorg.getNseq()+"-"+cnt_);//待选案源编号
    		}else{
    			return this.returnInfoForJS(false, "对不起，纳税人所属征管局信息错误，请登陆核心征管确认纳税人所属征管局是否正确.");
    		}
    		return writeHTML(JSONObject.fromObject(po).toString());
        	
    	}
    	else
    		return this.returnInfoForJS(false, "对不起，没有获取到纳税人信息，请确认输入的纳税人编号是否正确,或者查询核心征管系统确认该纳税人编号是否存在.");
    }
	public CaseSourceApprove getAppr() {
		return appr;
	}
	public void setAppr(CaseSourceApprove appr) {
		this.appr = appr;
	}
	public CaseSourceDrawApprove getDrawAppr() {
		return drawAppr;
	}
	public void setDrawAppr(CaseSourceDrawApprove drawAppr) {
		this.drawAppr = drawAppr;
	}
	public void setReportIllegalLogic(IReportIllegalLogic reportIllegalLogic) {
		this.reportIllegalLogic = reportIllegalLogic;
	}

}

